//
//  WBEmotionTabBar.m
//  新浪微博项目
//
//  Created by neng on 15/12/21.
//  Copyright (c) 2015年 neng. All rights reserved.
//

#import "WBEmotionTabBar.h"
#import "WBEmotionTabBarButton.h"

@interface WBEmotionTabBar ()

@property (nonatomic,weak) UIButton *selectedBtn;

@end

@implementation WBEmotionTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        //1.创建四个按钮
        [self setUpBtnWith:@"最近" type:WBEmotionTabBarButtonTypeRecent];
        self.selectedBtn = [self setUpBtnWith:@"默认" type:WBEmotionTabBarButtonTypeDefault];
        [self setUpBtnWith:@"emoji" type:WBEmotionTabBarButtonTypeEmoji];
        [self setUpBtnWith:@"浪小花" type:WBEmotionTabBarButtonTypeLxh];

    }
    return self;
}

//当代理不为空时，设置默认选中按钮
- (void)setDelegate:(id<WBEmotionTabBarDelegate>)delegate
{
    _delegate = delegate;
    
    [self btnClick:self.selectedBtn];
}

//设置一个按钮
- (WBEmotionTabBarButton *)setUpBtnWith:(NSString *)title type:(WBEmotionTabBarButtonType)type
{
    WBEmotionTabBarButton *btn = [WBEmotionTabBarButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    btn.tag = type;
    
    //监听事件
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:btn];

    return btn;
}
//按钮点击
- (void)btnClick:(UIButton *)btn
{
    
    self.selectedBtn.enabled = YES;
    [self.selectedBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    //为了使选中按钮再次点击不会重复
    btn.enabled = NO;
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];
    self.selectedBtn = btn;
    
    if ([self.delegate respondsToSelector:@selector(emotionTabBar:didClickedBtnWithType:)]) {
        [self.delegate emotionTabBar:self didClickedBtnWithType:(int)self.selectedBtn.tag];
    }
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = self.width / self.subviews.count;
    CGFloat height = self.height;
    
    for (int i = 0; i < self.subviews.count; i++) {
        UIButton *btn = self.subviews[i];
        btn.frame = CGRectMake(i * width, 0, width, height);
        
        NSString *nor = nil;
        NSString *disable = nil;
        if (i == 0) {
            nor = @"compose_emotion_table_left_normal";
            disable = @"compose_emotion_table_left_selected";
            
        }else if (i == self.subviews.count) {
            nor = @"compose_emotion_table_right_normal";
            disable = @"compose_emotion_table_right_selected";
        }else{
            nor = @"compose_emotion_table_mid_normal";
            disable = @"compose_emotion_table_right_selected";
        }
        UIImage *norImg = [UIImage imageWithStretchableName:nor];
        UIImage *disableImg = [UIImage imageWithStretchableName:disable];
        [btn setBackgroundImage:norImg forState:UIControlStateNormal];
        [btn setBackgroundImage:disableImg forState:UIControlStateDisabled];
    }
}

@end
